angular.module('app').controller('vmAddOpenstackCtrl', ['$rootScope', '$scope','$state','httpLoad','$stateParams','$modal',function($rootScope, $scope,$state,httpLoad,$stateParams,$modal) {
    $rootScope.link = '/statics/css/user.css';
    $rootScope.moduleTitle = '平台管理 > OpenStack';
    $scope.param = {
        rows: 10
    };
    $scope.showDetail = 1;
    $scope.showOpenstack=1;

    $scope.goBack = function(){
        $state.go('app.config.openstackTab');
        sessionStorage.setItem('openstacktabLocation', JSON.stringify('vmAdd'));
        sessionStorage.setItem('openstackTabvendor', JSON.stringify({id:$stateParams.id}));
    };

    (function(){
        $scope.addData = {};$scope.param={};$scope.isselectSize = false;
        //$scope.account = 'root';
        //获取地域数据
        $scope.getRegion = function(){
            httpLoad.loadData({
                url:'/region/list',
                method: 'POST',
                data: {"id":$stateParams.id},
                success:function(data){
                    if(data.success){
                        $scope.regionData = data.data;
                        angular.forEach($scope.regionData, function(data,index){
                            //默认选中第一个区域
                            if(index==0){
                                data.isRegionActive = true;
                                $scope.addData.region = data.id;
                            }
                            else data.isRegionActive = false;
                        });
                    }
                }
            });
        };
        if($scope.showOpenstack==1) $scope.getRegion();
        //选择地域
        $scope.chooseRegion1 = function(item){
            angular.forEach($scope.regionData, function(data,index){
                data.isRegionActive = false;
                if(data.id==item.id){
                    data.isRegionActive = !item.isRegionActive;
                }
                if(data.isRegionActive){
                    $scope.addData.region = data.id;
                }
            });
        };
        //获取镜像数据
        $scope.getImageData = function(){
            var params = {
                    simple : true
                },
                searchParam = [{"param":{"vendorId":$stateParams.id,"status":"ACTIVE"},"sign":"EQ"}];
            params.params = JSON.stringify(searchParam);
            httpLoad.loadData({
                url:'/image/list',
                method:'POST',
                data: params,
                noParam: true,
                success:function(data){
                    if(data.success){
                        $scope.imageList = data.data.rows;
                    }
                }
            });
        };
        if($scope.showOpenstack==1) $scope.getImageData();
        //获取网络数据
        $scope.getNetworkData = function(){
            var params = {
                    simple : true
                },
                searchParam = [{"param":{"vendorId":$stateParams.id},"sign":"EQ"}];
            params.params = JSON.stringify(searchParam);
            httpLoad.loadData({
                url:'/network/list',
                method:'POST',
                data: params,
                noParam: true,
                success:function(data){
                    if(data.success){
                        $scope.networkList = data.data.rows;
                        angular.forEach($scope.networkList, function(data,index){
                            //默认选中第一个
                            if(index==0){
                                data.isNetworkActive = true;
                                $scope.addData.networkId = data.id;
                            }
                            else data.isNetworkActive = false;
                        });
                    }
                }
            });
        };
        if($scope.showOpenstack==1) $scope.getNetworkData();
        //获取配置实例数据
        $scope.getSizeData = function(){
            var params = {
                    simple : true
                },
                searchParam = [{"param":{"vendorId":$stateParams.id},"sign":"EQ"}];
            params.params = JSON.stringify(searchParam);
            httpLoad.loadData({
                url:'/flavor/list',
                method: 'POST',
                data: params,
                noParam: true,
                success:function(data){
                    if(data.success){
                        $scope.sizeList = data.data.rows;
                    }
                }
            });
        };
        if($scope.showOpenstack==1) $scope.getSizeData();
        //选择CPU核数和内存大小
        $scope.slider = {
            options: {
                showTicks: true,
                readOnly: true,
                stepsArray : [
                    {value:"1"},
                    {value:"2"},
                    {value:"4"},
                    {value:"8"},
                    {value:"16"}
                ],
                translate: function(value) {
                    return value+'核';
                },
                onChange : function(sliderId, modelValue, highValue, pointerType){
                    var str = modelValue;
                    $scope.cpu = str.substring(0,str.length-1);
                }
            }
        };
        //$scope.slider1 = {
        //    options: {
        //        showTicks: true,
        //        readOnly: true,
        //        stepsArray : [
        //            {value: 0.5},
        //            {value: 2},
        //            {value: 4},
        //            {value: 6},
        //            {value: 8},
        //            {value: 16}
        //        ],
        //        translate: function(value) {
        //            return value+'G';
        //        },
        //        onChange : function(sliderId, modelValue, highValue, pointerType){
        //            //var str = modelValue;
        //            //$scope.memory = str.substring(0,str.length-1);
        //        }
        //    }
        //};

        //选择可用区域
        $scope.chooseRegion2 = function(item){
            angular.forEach($scope.regionData, function(data,index){
                data.isRegionActive = false;
                if(data.id==item.id){
                    data.isRegionActive = !item.isRegionActive;
                }
                if(data.isRegionActive){
                    $scope.addData.region = data.id;
                }
            });
        };
        //选择网络
        $scope.chooseNetwork = function(item,$index){
            angular.forEach($scope.networkList, function(data,index){
                if(data.id==item.id){
                    data.isNetworkActive = !item.isNetworkActive;
                }
            });
        };
        //选择配置实例
        $scope.selectSize = function(row){
            if(row==undefined) return;
            if(row!=""){
                var data = JSON.parse(row);
                $scope.addData.flavorId = data.flavorId;
                $scope.disk = data.disk+'';
                $scope.slider.value = data.cpu+'';$scope.cpu = data.cpu+'';
                $scope.memory = data.memory+'';
                //$scope.slider1.value = data.memory/1024;$scope.memory = data.memory;
                $scope.isselectSize = true;$scope.slider.options.readOnly = true;
                //$scope.slider1.options.readOnly = true;
            }
        };
        $scope.ok = function(){
            //云供应商的ID
            $scope.addData.id = $stateParams.id;
            //处理网络数据
            $scope.addData.networkId = '';
            angular.forEach($scope.networkList, function(data,index){
                if(data.isNetworkActive){
                    $scope.addData.networkId = $scope.addData.networkId + ',' + data.value;
                }
            });
            $scope.addData.networkId = $scope.addData.networkId.substr(1);
            if(!$scope.addData.region){
                $scope.pop('请选择地域','error');
                return;
            }
            if(!$scope.addData.imageId){
                $scope.pop('请选择镜像','error');
                return;
            }
            if($scope.addData.networkId==''){
                $scope.pop('请选择网络','error');
                return;
            }
            if(!$scope.addData.flavorId){
                $scope.pop('请选择配置实例','error');
                return;
            }
            httpLoad.loadData({
                url:'/vm/create',
                method:'POST',
                data: $scope.addData,
                success:function(data){
                    if(data.success){
                        $scope.pop(data.message);
                        $scope.goBack();
                    }
                }
            });
        };
        $scope.cancel = function(){
            $scope.goBack();
        };
    })();
}]);